<?php

namespace app\admin\controller;

use app\admin\logic\Brandlogic;
use app\admin\logic\Gcategorylogic;
use app\admin\logic\GoodsAddlogic;
use app\admin\logic\Goodslogic;
use app\admin\logic\Goodsproplogic;
use app\admin\model\Gbrand;
use app\admin\model\GgoodsProp;
use app\admin\model\Gcategory;

use app\admin\model\Goods;
use app\admin\model\GoodsPropValue;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Good extends BaseController
{

    // 商品列表
    public function goodList()
    {

        $param = Request::param();
        $cid=Request::get('cid');
        // 拼装搜索器条件
        $sdata=Goodslogic::setSearchSdata($param);
        // 留值设置
        $data = Goodslogic::setData($param);
        View::assign($data);
        // 时间排序
        list($order,$cid,$msg) = Goodslogic::getTimeOrder($cid);
        View::assign('msg',$msg);
        // 查询数据
        $sel = Goods::getGoodsSel($sdata,$order);
        // 品牌查询
        $brand = Gbrand::getBrandSel([],[]);
        // 分类查询
        $cate = Goodslogic::getCate();
        // 分页
        $page=$sel->render();
        View::assign('page',$page);
        // 渲染数据
        View::assign('cate',$cate);
        View::assign('brand',$brand);
        View::assign('sel',$sel);
        // 时间排序
        View::assign('cid',$cid);

        return View::fetch();
    }

    // 商品添加列表
    public function goodAdd()
    {
        if (Request::isPost()){
            // 获取数据
            $param = Request::post();
            $add = GoodsAddlogic::getGoodsAddInfo($param);
            list($res,$msg) = Goods::addGoodsInfo($add);
            if ($res){
                $this->success($msg,url('goodList'));
            }else{
                $this->error('网络异常,添加失败');
            }
        }



        // 品牌查询
        $brand = Gbrand::getBrandSel([],[]);
        View::assign('brand',$brand);
        // 分类查询
        $cate = Goodslogic::getCate();
        View::assign('cate',$cate);

        return View::fetch();
    }

    // 商品修改
    public function goodEdit()
    {
        if (Request::isGet()){
            $param=Request::param();
            $one=Goods::find($param['goods_id']);
//            echo "<pre>";
//            print_r($one->toArray());
//            echo "</pre>";
            View::assign('one',$one);
        }else{
            $param=Request::param();

            list($res,$msg)=Goods::goodsEdit($param);
            if($res){
                $this->success($msg,'goodList');
            }else{
                $this->error('修改失败');
            }
        }
        // 品牌查询
        $brand = Gbrand::getBrandSel([],[]);
        View::assign('brand',$brand);
        // 分类查询
        $cate = Goodslogic::getCate();
        View::assign('cate',$cate);
        return View::fetch();
    }

    // 分类表
    public function cate()
    {
        // 接参
        $param = Request::param();
        // 拼装查询条件  判断当前parent_id是多少
        $where = Gcategorylogic::reformData($param);
        // 查询所有的一级分类/(根据父ID查询)
        $sel = Gcategory::getCateSel($where);
        // 查询分类下是否有下一级,有下一级赋值cate_name
        $sel = Gcategorylogic::setCateNext($sel);

        View::assign('sel',$sel);
        return View::fetch();
    }

    // 分类修改表
    public function cateedit()
    {
        if(Request::isGet()){
            $param=Request::param();
            $one=Gcategory::find($param['cate_id']);
            View::assign('one',$one);
            echo "<pre>";
            print_r($one->toArray());
            echo "</pre>";
        }else{
            $param=Request::param();
            list($res,$msg)=Gcategory::cateEdit($param);

            if($res){
                $this->success($msg,'cate');
            }else{
                $this->error($msg);
            }
        }

        // 分类查询
        $cate = Goodslogic::getCate();
        View::assign('cate',$cate);
        return View::fetch();
    }

    // 属性表
    public function attribute()
    {
        // 接值
        $param = Request::param();
        // 留值
        $data=Goodsproplogic::getDataAttribute($param);
        View::assign($data);
        // 拼接where条件
        list($where,$whereOr)=Goodsproplogic::getWereAttribute($param);
        // 查询数据库
        $sel = GgoodsProp::getGoodsPropSel($where,$whereOr);
        $gpv = GoodsPropValue::getGoodsPropValueSel($where);
        // pvalues变中文
        $sel = Goodsproplogic::getGoodsPropName($sel,$gpv);

        // 渲染数据
        View::assign('sel',$sel);
        View::assign('gpv',$gpv);
        return View::fetch();
    }

    // 品牌表
    public function brand()
    {
        // 接值
        $param = Request::param();

        // 留值设置
        $data = Brandlogic::GetDataBrand($param);
        View::assign($data);

        // 接值 拼接where
        list($where,$whereOr)=Brandlogic::getWhereBrand($param);

        // 查询数据
        $sel = Gbrand::getBrandSel($where,$whereOr);

        View::assign('sel',$sel);

        // 分页
        $page=$sel->render();
        View::assign('page',$page);

        return View::fetch();
    }

    //品牌添加
    public function brandAdd(){

        if (Request::isPost()){
            // 获取数据
            $param = Request::post();
            $data = Brandlogic::getBrandData($param);
            list($res,$msg) = Gbrand::addBrandInfo($data);
            if ($res){
                $this->success($msg,url('brand'));
            }else{
                $this->error($msg,'网络异常,添加失败');
            }
        }

        return View::fetch();
    }

    //品牌修改
    public function brandEdit()
    {
        if(Request::isGet()){
            $param=Request::param();
            $one=Gbrand::find($param['brand_id']);

            View::assign('one',$one);
        }else{
            $param=Request::param();
            list($res,$msg)=Gbrand::brandEdit($param);

            if($res){
                $this->success($msg,'brand');
            }else{
                $this->error($msg);
            }
        }

        return View::fetch();
    }
}